<template>
	<!-- 资产盘点（列表） -->
	<view class="my-apply">
		<!-- <view class="top-head" :style="{height:`${44+safeTop}px`,paddingTop:`${safeTop}px`}">
			<text class="iconfont icon-left-arrow" @click="goBack"></text>
			<text class="title">资产盘点</text>
			<text></text>
			<text class="iconfont icon-scan" @click="toScan"></text>
		</view> -->
		<picker :range="pickerRange" @change="bindPickerChange" :disabled="pickerDisabled">
			<view class="tab-posi">
				<NewSegmentedControl :current="current" :values="items" style-type="text" active-color="#198a6c"
					@clickItem="onClickItem" />
			</view>
		</picker>
		<view class="content">
			<view class="card" @click="toDetail(item.checkStoreId,item.version)" v-for="item in myApplyList"
				:key="item.assetCheckId">
				<view class="head">
					<text>{{item.assetCheckName}}</text>
					<text :class="{checking:item.statusCh=='盘点中',haveinventory:item.status==8}">{{item.statusCh}}</text>
				</view>
				<view class="list">
					<view>盘点编号：{{item.assetCheckNo}}</view>
					<view>盘点单位：{{item.storeName}}</view>
					<view>盘点人：{{item.checkerCh||'--'}}</view>
					<view>盘点期限：{{item.assetCheckExpectDate}}</view>
					<view class="remark">备注信息：{{item.remark||'--'}}</view>
					<text class="iconfont icon-jinrujiantouxiao"></text>
				</view>
			</view>
		</view>
		<!-- 加载结束 -->
		<view class="uni-loadmore" v-if="showLoadMore">{{loadMoreText}}</view>
	</view>
</template>

<script setup lang="ts">
	import {
		ref
	} from "vue"
	import {
		onPullDownRefresh,
		onReachBottom,
		onShow,
		onBackPress
	} from "@dcloudio/uni-app";
	import {
		assetCheckList,
		getAllEnum,
		checkNameList
	} from '@/common/api/assetCheckAPI.ts'
	import {
		addrList,
		applyPerson
	} from "@/common/api/myApply";
	import {
		CONSTANT
	} from "@/common/config/config.ts";
	import {
		fromSearch
	} from "@/common/util/tabSearchUse.ts";
	const safeTop = ref(uni.getSystemInfoSync().safeArea.top)
	const current = ref(0);
	const items = ref(["盘点单位", "盘点名称", "盘点人", "盘点状态"]);
	const pickerRange = ref()
	const pickerDisabled = ref(false)
	const currentTab = ref(0)
	const myApplyList = ref([]);
	const address = ref([])
	const checkName = ref([])
	const person = ref([])
	const status = ref([])
	const page = ref(CONSTANT.initPage);
	const rows = ref(CONSTANT.pageSize);
	const allPages = ref(0);
	const showLoadMore = ref(false);
	const loadMoreText = ref("加载中...");
	const searchParams = ref({
		storeId: '',
		checker: '',
		status: null,
		assetCheckName: ''
	});
	const matchRule = {
		storeId: 'eq',
		checker: 'eq',
		status: 'eq',
		assetCheckName: 'cn'
	};
	interface EnumType {
		label: string,
			value: string
	}
	const goBack = ()=>{
		uni.navigateBack({
			delta:1
		})
	}
	// const toScan = ()=>{
	// 	uni.navigateTo({
	// 		url:`/pages/qrCode/qrCode?tip=结束扫码&successUrl=/pages/checkAssets/checkDetail/index&clickTipUrl=/pages/checkAssets/checkDetail/index`
	// 	})
	// }
	onBackPress((e)=>{
		uni.switchTab({
			url:`/pages/index/index`
		})
		// uni.navigateTo({
		// 	url: '/pages/index/index'
		// });
		return true;
	})
	onShow(()=>{
		getApplyList()
	})
	const getApplyList = () => {
		const params = Object.assign(fromSearch(searchParams.value, matchRule), {
			page: page.value,
			rows: rows.value,
		});
		loadMoreText.value = '加载中...'
		assetCheckList(params).then((res: any) => {
			uni.stopPullDownRefresh();
			if (res.code === 0) {
				allPages.value = res.data.pages;
				page.value = res.data.current;
				myApplyList.value = res.data.records;
				myApplyList.value.forEach((item: any) => {
					item.assetCheckExpectDate = item.assetCheckExpectDate?.replace('T', ' ')
				})
				if (myApplyList.value.length === 0) {
					showLoadMore.value = true;
					loadMoreText.value = "暂无数据";
				} else if (myApplyList.value.length < CONSTANT.pageSize) {
					showLoadMore.value = true;
					loadMoreText.value = "没有更多的数据了";
				} else {
					showLoadMore.value = false;
				}
			} else {
				uni.showToast({
					icon: "none",
					title: res.message,
				});
			}
		})
	}
	getApplyList()
	const getApplyAddr = () => {
		addrList().then((res: any) => {
			if (res.code == 0) {
				address.value = res.data.records.map((item: any) => {
					return {
						label: item.storeName,
						value: item.id
					}
				})
			}
		})
		getAllEnum('*').then((res: any) => {
			if (res.code == 0) {
				status.value = res.data['AssetCheckStatus'].map((item: any) => {
					return {
						label: item.value,
						value: item.code
					}
				})
			}
		})
		applyPerson().then((res: any) => {
			if (res.code == 0) {
				person.value = res.data.records.map((item: any) => {
					return {
						label: item.nickName,
						value: item.id
					}
				})
			}
		})
		checkNameList().then((res: any) => {
			if (res.code == 0) {
				checkName.value = res.data
			}
		})
	}
	getApplyAddr()
	const onClickItem = (e: {
		currentIndex: number
	}) => {
		page.value = CONSTANT.initPage
		currentTab.value = e.currentIndex
		switch (e.currentIndex) {
			case 0:
				pickerRange.value = address.value.map((item: EnumType) => item.label);
				pickerRange.value.push('全部')
				break
			case 1:
				pickerRange.value = checkName.value;
				pickerRange.value.push('全部')
				break;
			case 2:
				pickerRange.value = person.value.map((item: EnumType) => item.label);
				pickerRange.value.push('全部')
				break
			case 3:
				pickerRange.value = status.value.map((item: EnumType) => item.label);
				pickerRange.value.push('全部')
				break
		}
	};
	const bindPickerChange = (e: any) => {
		let len = pickerRange.value.length-1
		myApplyList.value = []
		switch (currentTab.value) {
			case 0:
				items.value[0] = e.detail.value==len?'盘点单位':pickerRange.value[e.detail.value];
				searchParams.value.storeId = e.detail.value==len?'':(address.value[e.detail.value] as EnumType).value;
				break;
			case 1:
				items.value[1] = e.detail.value==len?'盘点名称':pickerRange.value[e.detail.value];
				searchParams.value.assetCheckName = e.detail.value==len?'':checkName.value[e.detail.value]
				break;
			case 2:
				items.value[2] = e.detail.value==len?'盘点人':pickerRange.value[e.detail.value];
				searchParams.value.checker = e.detail.value==len?'':(person.value[e.detail.value] as EnumType).value
				break;
			case 3:
				items.value[3] = e.detail.value==len?'盘点状态':pickerRange.value[e.detail.value];
				searchParams.value.status = e.detail.value==len?null:(status.value[e.detail.value] as EnumType).value
				break;
		}
		getApplyList()
	}
	const toDetail = (id: string, v: number) => {
		uni.navigateTo({
			url: `/pages/checkAssets/checkDetail/index?checkStoreId=${id}&version=${v}`
		})
	}
	onPullDownRefresh(() => {
		page.value = CONSTANT.initPage;
		myApplyList.value = [];
		getApplyList();
	});
	onReachBottom(() => {
		if (page.value > allPages.value || page.value == allPages.value) {
			loadMoreText.value = '没有更多的数据了'
			showLoadMore.value = true;
		} else {
			page.value++;
			loadMoreText.value = '加载中……';
			showLoadMore.value = true;
			getApplyList();
		}

	})
</script>

<style scoped lang="scss">

</style>
